package cn.stylefeng.guns.modular.wms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.ums.entity.Member;
import cn.stylefeng.guns.modular.ums.model.params.MemberParam;
import cn.stylefeng.guns.modular.ums.service.MemberService;
import cn.stylefeng.guns.modular.wms.entity.Resume;
import cn.stylefeng.guns.modular.wms.mapper.ResumeMapper;
import cn.stylefeng.guns.modular.wms.model.params.*;
import cn.stylefeng.guns.modular.wms.model.result.*;
import cn.stylefeng.guns.modular.wms.service.*;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 个人简历表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2021-04-02
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements ResumeService {

    @Resource
    private WorkExpService workExpService;
    @Resource
    private ProExpService proExpService;
    @Resource
    private EduExpService eduExpService;
    @Resource
    private JobExpectService jobExpectService;
    @Resource
    private MemberService memberService;



    @Override
    public void add(ResumeParam param){
        Resume entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ResumeParam param){
        this.removeById(getKey(param));
    }

    @Transactional
    @Override
    public void update(ResumeParam param){
        //判断ID是否存在，如果不存在，则新建
        ResumeResult resumeResult=findBySpec(param);
        if (resumeResult!=null){
            param.setId(resumeResult.getId());
            Resume oldEntity = getOldEntity(param);
            Resume newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        }else{
            this.add(param);
        }
        //更新实名信息
        MemberParam memberParam=new MemberParam();
        memberParam.setName(param.getName());
        memberParam.setMemberId(param.getMemberId().intValue());
        memberParam.setAvatar(param.getAvatar());
        memberService.update(memberParam);
    }

    @Override
    public ResumeResult findBySpec(ResumeParam param){
        return baseMapper.findBySpec(param);
    }

    @Override
    public ResumeResult getResume(ResumeParam param) {
        ResumeResult resume=this.findBySpec(param);
        WorkExpParam workExpParam=new WorkExpParam();
        workExpParam.setMemberId(resume.getMemberId());
        List<WorkExpResult> workExpResult=workExpService.findListBySpec(workExpParam);
        resume.setWorkExpList(workExpResult);
        ProExpParam proExpParam=new ProExpParam();
        proExpParam.setMemberId(resume.getMemberId());
        List<ProExpResult> proExpResults=proExpService.findListBySpec(proExpParam);
        resume.setProExpList(proExpResults);
        EduExpParam eduExpParam=new EduExpParam();
        eduExpParam.setMemberId(resume.getMemberId());
        List<EduExpResult> eduExpResults=eduExpService.findListBySpec(eduExpParam);
        resume.setEduExpList(eduExpResults);
        JobExpectParam jobExpectParam=new JobExpectParam();
        jobExpectParam.setMemberId(resume.getMemberId());
        List<JobExpectResult> jobExpectResults=jobExpectService.findListBySpec(jobExpectParam);
        resume.setJobExpectList(jobExpectResults);
        Member member=memberService.getById(resume.getMemberId());
        resume.setMemberSn(member.getMemberSn());
        return resume;
    }

    @Override
    public List<ResumeResult> findListBySpec(ResumeParam param){
        return baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ResumeParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList2(pageContext, param);
//        List<ResumeResult> list=page.getRecords();
//        for(int i=0;i<list.size();i++){
//            WorkExpParam workExpParam=new WorkExpParam();
//            workExpParam.setMemberId(list.get(i).getMemberId());
//            List<WorkExpResult> workExpResultList=workExpService.findListBySpec(workExpParam);
//            if (workExpResultList!=null && workExpResultList.size()>0){
//                list.get(i).setWorkExpList(workExpResultList);
//            }else{
//                EduExpParam eduExpParam=new EduExpParam();
//                eduExpParam.setId(list.get(i).getMemberId());
//                List<EduExpResult> eduExpResults=eduExpService.findListBySpec(eduExpParam);
//                list.get(i).setEduExpList(eduExpResults);
//            }
//        }
        return LayuiPageFactory.createPageInfo(page);
    }
    @Override
    public LayuiPageInfo findPageBySpecForAdmin(ResumeParam param){
        Page pageContext = getPageContext();
        IPage<ResumeResult> page = this.baseMapper.customPageList(pageContext, param);
        page.getRecords().forEach(item -> {
            List<JobExpectResult> jobExpectResults=jobExpectService.findListBySpec(new JobExpectParam().setMemberId(item.getMemberId()));
            if (jobExpectResults.size()>0){
                item.setExpectSalary(jobExpectResults.get(0).getExpectSalary());
                item.setExpectPostName(jobExpectResults.get(0).getPostName());
            }
        });
        return LayuiPageFactory.createPageInfo(page);
    }


    private Serializable getKey(ResumeParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Resume getOldEntity(ResumeParam param) {
        return this.getById(getKey(param));
    }

    private Resume getEntity(ResumeParam param) {
        Resume entity = new Resume();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
